package com.xander.qqdatabase.controller;

import com.xander.qqdatabase.base.ResultBean;
import com.xander.qqdatabase.entity.User;
import com.xander.qqdatabase.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author 叶振东
 * @date 2019-01-10
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("login")
    public ResultBean login(User user, HttpSession session) {
        User getUser = userService.login(user.getUsername(), user.getPassword());
        session.setAttribute("user", getUser);
        return new ResultBean();
    }

    @PostMapping("register")
    public ResultBean register(HttpSession session, User user, String verCode) {
        String code = (String) session.getAttribute("captcha");
        this.userService.register(user, code, verCode);
        return new ResultBean<>();
    }
}
